<template>
  <div class="usermanage-page">
    <el-card>
      <div class="header">
        <span class="title">用户管理</span>
        <el-button type="primary" @click="openAddDialog">新增用户</el-button>
      </div>
      <el-table :data="userList" style="width: 100%" border>
        <el-table-column prop="id" label="ID" width="80" align="center"/>
        <el-table-column prop="username" label="用户名" align="center"/>
        <el-table-column prop="email" label="邮箱" align="center"/>
        <el-table-column prop="role" label="角色" align="center"/>
        <el-table-column label="操作" width="200" align="center">
          <template #default="scope">
            <el-button size="small" @click="openEditDialog(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteUser(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑用户弹窗 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="400px">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" placeholder="请选择角色">
            <el-option label="管理员" value="admin"></el-option>
            <el-option label="用户" value="user"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const userList = ref([
  { id: 1, username: 'admin', email: 'admin@example.com', role: 'admin' },
  { id: 2, username: 'user1', email: 'user1@example.com', role: 'user' },
  { id: 3, username: 'user2', email: 'user2@example.com', role: 'user' }
])

const dialogVisible = ref(false)
const dialogTitle = ref('新增用户')
const isEdit = ref(false)
const formRef = ref(null)
const form = reactive({
  id: null,
  username: '',
  email: '',
  role: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ]
}

function openAddDialog() {
  dialogTitle.value = '新增用户'
  isEdit.value = false
  Object.assign(form, { id: null, username: '', email: '', role: '' })
  dialogVisible.value = true
}

function openEditDialog(row) {
  dialogTitle.value = '编辑用户'
  isEdit.value = true
  Object.assign(form, row)
  dialogVisible.value = true
}

function submitForm() {
  formRef.value.validate((valid) => {
    if (valid) {
      if (isEdit.value) {
        // 编辑用户
        const idx = userList.value.findIndex(u => u.id === form.id)
        if (idx !== -1) {
          userList.value[idx] = { ...form }
          ElMessage.success('用户信息已更新')
        }
      } else {
        // 新增用户
        const newId = userList.value.length ? Math.max(...userList.value.map(u => u.id)) + 1 : 1
        userList.value.push({ ...form, id: newId })
        ElMessage.success('用户已新增')
      }
      dialogVisible.value = false
    }
  })
}

function deleteUser(row) {
  ElMessageBox.confirm(`确定要删除用户 "${row.username}" 吗？`, '提示', {
    type: 'warning'
  }).then(() => {
    userList.value = userList.value.filter(u => u.id !== row.id)
    ElMessage.success('用户已删除')
  }).catch(() => {})
}
</script>

<style scoped>
.usermanage-page {
  max-width: 900px;
  margin: 40px auto;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
}
.title {
  font-size: 20px;
  font-weight: bold;
}
</style>
